package com.project.service;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import com.project.dao.impl.UserDaoImpl;
import com.project.server.Request;
import com.project.server.Response;
import com.project.server.Servlet;

import java.util.List;

/**
 * 查询所有用户的业务组件
 */
public class FindAllService implements Servlet {
    /**
     * 聚合持久组件
     */
    private IUserDao dao = new UserDaoImpl();

    public void service(Request request, Response response){
        //调用持久方法得到查询结果
        List<UserBean> userList = dao.findAll();

        String str = "<table border='1' width='50%'>" +
                "<thead><tr><th>编号</th><th>用户名</th><th>密码</th>" +
                "<th>生日</th><th>电话</th><th>操作</th></tr></thead>";
        str += "<tbody>";

        for (UserBean user : userList){
            str += "<tr><td>"+user.getId()+"</td><td>"+user.getUserName()+"</td>" +
                    "<td>"+user.getPwd()+"</td><td>"+user.getBirthday()+"</td>" +
                    "<td>"+user.getPhone()+ "</td>" +
                    "<td><a href='del?id="+user.getId()+"'>删除 " +
                    "<a href='findById?id="+user.getId()+"'>修改</td></tr>";
        }
        str += "</tbody></table>";

        str += "<a href='add.html'>添加用户</a>";
        response.sendMessage(str);

    }
}
